<?php
/**
 * 文章阅读数量统计
 * @author adin283@163.com
 * @create 2013-07-27 10:44:03
 */
class Business_ReadStat
{
	protected static $_instance;
	
	protected $_postId;
	
	public function __construct()
	{
		$this->_tableInfo = Zend_Registry::get('database')->readstat;
		$this->_table = Zend_Registry::get('database')->readstat->tablename;
	}
	
	public static function getInstance()
	{
		if (null === self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	public function setPostId($postId)
	{
		$this->_postId = $postId;
		return $this;
	}
	
	public function getResult()
	{
		$data = array();
		$db = Utility_Db::getInstance()->conn();
		$where = array(
			$this->_tableInfo->postid . ' = ?' => $this->_postId
		);
		$data[$this->_tableInfo->readcount] =
			new Zend_Db_Expr($this->_tableInfo->readcount . ' + 1');
		$data[$this->_tableInfo->mtime] = date("Y-m-d H:i:s");
		$affectRows = $db->update($this->_table, $data, $where);
		if (!$affectRows) {
			$data[$this->_tableInfo->ctime] = date("Y-m-d H:i:s");
			$data[$this->_tableInfo->readcount] = 1;
			$data[$this->_tableInfo->postid] = $this->_postId;
			$insRowNum = $db->insert($this->_table, $data);
		}
		if (!isset($insRowNum) && !isset($affectRows)) {
			return false;
		} else {
			return true;
		}
	}
}